package com.fruit.controller.wap.pay;
  
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.pay.PayInfo;
import com.fruit.resolver.DefiniteException;
import com.fruit.service.pay.PayHandel;
import com.fruit.service.pay.PayManager;
import com.fruit.util.CommonUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 支付controller
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller("wapPayController")
@RequestMapping(value = "/wap/pay")
public class PayController extends BaseController {

	@Autowired
	private PayHandel payHandelService;
	
	@Autowired
	private PayManager payService;

	@Value("${h5_domain}")
	private String h5Domain;

	@Value("${domain}")
	private String domain;

	/**
	 * 确认订单  <br/>
	 * @param pi 确认订单信息
	 * @param session session
	 * @return 确认订单页
	 */
	@RequestMapping(value = "checkParams", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> checkParams(@RequestBody PayInfo pi, HttpSession session) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		String tradeId = CommonUtil.getUUID();
		Integer userId = (Integer) session.getAttribute("userId");
		pi.setUserId(userId);
		log.info("[{}]-【校验参数】-【用户：{}】-【开始】", tradeId, pi.getUserId());
		resMap = payService.findCheckGoodsParams(tradeId, pi);
		ResultInfo ri = (ResultInfo) resMap.get("result");
		if (ResultInfo.ERROR.equals(ri.getState())) {
			if (ri.getCode().equals("user_not_real")) {
				log.info("[{}]-【校验参数】-【用户未认证】", tradeId);
				String userRealUrl = (String) resMap.get("userRealUrl");
				log.error("[{}]-【校验参数】-【异常地址：{}】", tradeId, userRealUrl);
				resMap.put("jump_url", userRealUrl + "?returnUrl=" + domain + "/wap/goods/detail?id="
						+ pi.getGoodsTimeId());
			} else {
				log.error("[{}]-【校验参数】-【异常】-【{}：{}】", tradeId, ri.getCode(), ri.getMessage());
			}
		} else {
			log.info("[{}]-【校验参数】-【成功】", tradeId);
		}
		return resMap;
	}	
	
	/**
	 * 确认订单  <br/>
	 * @param pi 确认订单信息
	 * @param session session
	 * @return 确认订单页
	 */
	@RequestMapping(value = "firmOrder", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView firmOrder(PayInfo pi, HttpSession session) {
		ModelAndView view = this.getModelAndView();
		String tradeId = CommonUtil.getUUID();
		Integer userId = (Integer) session.getAttribute("userId");
		pi.setUserId(userId);
		log.info("[{}]-【确认订单】-【用户：{}】-【开始】", tradeId, pi.getUserId());
		Map<String, Object> resMap = payService.findCheckFirmOrderParams(tradeId, pi);
		ResultInfo ri = (ResultInfo) resMap.get("result");
		if (ResultInfo.ERROR.equals(ri.getState())) {
			if (ri.getCode().equals("user_not_real")) {
				log.info("[{}]-【确认订单】-【用户未认证】", tradeId);
				String userRealUrl = (String) resMap.get("userRealUrl");
				log.error("[{}]-【确认订单】-【异常地址：{}】", tradeId, userRealUrl);
				view.setViewName("redirect:" + userRealUrl + "?returnUrl=" + domain + "/wap/goods/detail?id="
						+ pi.getGoodsTimeId());
			} else {
				log.error("[{}]-【确认订单】-【异常】-【{}：{}】", tradeId, ri.getCode(), ri.getMessage());
				throw new DefiniteException(ri.getMessage());
			}
		} else {
			log.info("[{}]-【确认订单】-【成功】", tradeId);
			view.addAllObjects(resMap);
			view.setViewName("order/firmOrder");
		}
		
		return view;
	}
	
	
	
	/**
	 * 订单提交 <br/>
	 * @param pi 支付信息
	 * @param session session
	 * @return 提交订单页
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	@RequestMapping(value = "submitOrder", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView submitOrder(PayInfo pi, HttpSession session) {
		String tradeId = CommonUtil.getUUID();
		Integer userId = (Integer) session.getAttribute("userId");
		pi.setUserId(userId);
		log.info("[{}]-【订单提交】-【用户：{}】-【开始】", tradeId, pi.getUserId());
		ModelAndView view = this.getModelAndView();
		log.info("[{}]-【订单提交】-【调用保存订单服务】", tradeId);
		Map<String, Object> resMap = payHandelService.saveSubmitOrder(tradeId, pi);
		ResultInfo ri = (ResultInfo) resMap.get("result");
		if (ResultInfo.ERROR.equals(ri.getState())) {
			if (ri.getCode().equals("user_not_real")) {
				log.info("[{}]-【订单提交】-【用户未认证】", tradeId);
				String userRealUrl = (String) resMap.get("userRealUrl");
				log.error("[{}]-【订单提交】-【异常地址：{}】", tradeId, userRealUrl);
				view.setViewName("redirect:" + userRealUrl + "?returnUrl=" + domain + "/wap/goods/detail?id="
						+ pi.getGoodsTimeId());
			} else {
				log.error("[{}]-【订单提交】-【异常】-【{}：{}】", tradeId, ri.getCode(), ri.getMessage());
				throw new DefiniteException(ri.getMessage());
			}
		} else {
			log.info("[{}]-【订单提交】-【成功】", tradeId);
			String transactionCode = (String) resMap.get("transactionCode");
			view.setViewName("redirect:/wap/lightpay/lightPayCashier?transactionCode=" + transactionCode);
		}

		log.info("[{}]-【订单提交】-【结束】", tradeId);
		return view;
	}

	
	/**
	 * 订单提交 <br/> 异步
	 * @param pi 支付信息
	 * @param session session
	 * @return 提交订单页
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	@RequestMapping(value = "submitOrderAsyn", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> submitOrderAsyn(PayInfo pi, HttpSession session) {
		Map<String, Object> resMap = new HashMap<>();
		String tradeId = CommonUtil.getUUID();
		Integer userId = (Integer) session.getAttribute("userId");
		pi.setUserId(userId);
		log.info("[{}]-【订单提交】-【用户：{}】-【开始】", tradeId, pi.getUserId());
		log.info("[{}]-【订单提交】-【调用保存订单服务】", tradeId);
		resMap = payHandelService.saveSubmitOrder(tradeId, pi);
		ResultInfo ri = (ResultInfo) resMap.get("result");
		if (ResultInfo.ERROR.equals(ri.getState())) {
			if (ri.getCode().equals("user_not_real")) {
				log.info("[{}]-【订单提交】-【用户未认证】", tradeId);
				String userRealUrl = (String) resMap.get("userRealUrl");
				log.error("[{}]-【订单提交】-【异常地址：{}】", tradeId, userRealUrl);
				resMap.put("jump_url", userRealUrl + "?returnUrl=" + domain + "/wap/goods/detail?id="
						+ pi.getGoodsTimeId());
			} else {
				log.error("[{}]-【订单提交】-【异常】-【{}：{}】", tradeId, ri.getCode(), ri.getMessage());
			}
		} else {
			log.info("[{}]-【订单提交】-【成功】", tradeId);
			String transactionCode = (String) resMap.get("transactionCode");
			resMap.put("jump_url", "wap/lightpay/lightPayCashier?transactionCode=" + transactionCode);
		}

		log.info("[{}]-【订单提交】-【结束】", tradeId);
		return resMap;
	}
}
